<template>
  <div class="content">
    <div class="category" >
<!--      'category__item--active':currentTab === item.tab判断选中项高亮-->
      <div :class="{'category__item':true,'category__item--active':currentTab === item.tab}" v-for="item in CATEGORIES" :key="item.tab" @click="()=>handleTabClick(item.tab)">{{item.name}}</div>
    </div>
    <div class="product">
      <div class="product__item" v-for="(item) in list" :key="item._id">
        <img class="product__item__img" :src="item.imgUrl">
        <div class="product__item__detail">
          <h4 class="product__item__title">{{item.name}}</h4>
          <p class="product__item__sales">月售{{item.sales}}件</p>
          <p class="product__item__price">
            <span class="product__item__yen">&yen;</span>{{item.price}}
            <span class="product__item__origin">&yen;{{item.oldPrice}}</span>
          </p>
        </div>
        <div class="product__number">
          <span class="product__number__minus" @click="()=>{changeCartItemInfo(shopId,item._id,item,-1)}">-</span>
<!--          从vuex拿到对应的count，通过cardList来查找shopid，在尝试去找productId-->
<!--          {{cartList?.[shopId]?.[item._id]?.count || 0}}-->
          {{cartList?.[shopId]?.[item._id]?.count || 0}}
          <span class="product__number__plus" @click="()=>{changeCartItemInfo(shopId,item._id,item,1)}">+</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { get } from '../../utils/request'
import { reactive, ref, toRefs, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import { useCommonCartEffect } from '@/views/shop/commonCartEffect'

const CATEGORIES = [
  {
    name: '全部商品',
    tab: 'all'
  },
  {
    name: '秒杀',
    tab: 'seckill'
  },
  {
    name: '新鲜水果',
    tab: 'fruit'
  }
]
// tab切换相关数据
const useTabEffect = () => {
  const currentTab = ref(CATEGORIES[0].tab)
  const handleTabClick = (tab) => {
    currentTab.value = tab
  }
  return { currentTab, handleTabClick }
}
const useCurrentListEffect = (currentTab, shopId) => {
  const content = reactive({ list: [] })
  const getContentData = async () => {
    const result = await get(`/api/shop/${shopId}/products`, { tab: currentTab.value })
    if (result?.data?.data?.length) {
      content.list = result.data.data
    }
  }
  watchEffect(() => {
    getContentData()
  })
  const { list } = toRefs(content)
  return { list }
}

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Content',
  setup () {
    const route = useRoute()
    const shopId = route.params.id
    const { currentTab, handleTabClick } = useTabEffect()
    const { list } = useCurrentListEffect(currentTab, shopId)
    const { changeCartItemInfo, cartList } = useCommonCartEffect()
    return { CATEGORIES, currentTab, handleTabClick, list, shopId, changeCartItemInfo, cartList }
  }
}
</script>

<style lang="scss" scoped>
.content{
  display: flex;
  position: absolute;
  left: 0;
  right: 0;
  top: 1.5rem;
  bottom: 0.5rem;
  .category{
    width: .76rem;
    height: 100%;
    background: #F5F5F5;
    overflow-y:scroll ;
    &__item{
      line-height: .4rem;
      text-align: center;
      font-size: 14px;
      color: #333;
      &--active{
        background: #fff;
      }
    }
  }
  .product{
    flex: 1;
    overflow-y: scroll;
    &__item{
      position: relative;
      display: flex;
      padding: .12rem 0;
      margin: 0 .16rem;
      border-bottom: .01rem solid #f1f1f1;
      &__img{
        width: .68rem;
        height: .68rem;
        margin-right: .16rem;
      }
      &__title{
        line-height: .2rem;
        font-size: .14rem;
        color: #333;
        margin: 0;
      }
      &__sales{
        margin: .06rem 0;
        line-height: .16rem;
        font-size: .12rem;
        color: #333;
      }
      &__price{
        margin: 0;
        line-height: .2rem;
        font-size: .14rem;
        color: #E93B3B;
      }
      &__yen{
        font-size: .12rem;
      }
      &__origin{
        line-height: .2rem;
        font-size: .12rem;
        color: #333;
        text-decoration: line-through;
      }
      .product__number{
        position: absolute;
        right: 0;
        bottom: .12rem;
        &__minus,&__plus{
          width: .2rem;
          height: .2rem;
          display: inline-block;
          border-radius: 50%;
          line-height: .2rem;
          text-align: center;
          font-size: .2rem;
        }
        &__minus{
          border:.01rem solid #666 ;
          color: #666;
          margin-right: .05rem;
        }
        &__plus{
          background: #0091FF;
          color: #fff;
          margin-left: .05rem;
        }
      }
    }
  }
}
</style>
